package com.nopay.dxdwphone.ui.ativity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.nopay.dxdwphone.base.BaseActivity;
import com.nopay.dxdwphone.bean.CommonResponse;
import com.nopay.dxdwphone.bean.request.UpdateUserInfoRequest;
import com.nopay.dxdwphone.Constants;
import com.nopay.dxdwphone.R;
import com.nopay.dxdwphone.databinding.ActivityCompanyBinding;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * @ProjectName: myVisualPhone
 * @Package: cn.cn.myvisualphone.ui.ativity
 * @ClassName: ForgetActivity
 * @Description: 更新用户姓名
 * @Author: anqi
 * @CreateDate: 2021/5/23 8:05
 * @UpdateUser: 更新者
 * @UpdateDate: 2021/5/23 8:05
 * @UpdateRemark: 更新说明
 * @Version: 1.0
 */
public class CompanyNameActivity extends BaseActivity<ActivityCompanyBinding> {

    @Override
    protected void init(Bundle savedInstanceState, Object... preData) {
        String companyName = SPUtils.getInstance().getString(Constants.SP_COMPANY_NAME, "");
        binding.et.setText(companyName);

        binding.rlRoot.setPadding(0, getStatusBarHeight(CompanyNameActivity.this), 0, 0);
        binding.ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mSwipeBackHelper.backward();

            }
        });
        binding.commitBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String company = binding.et.getText().toString().trim();
                if (TextUtils.isEmpty(company)){
                    ToastUtils.showShort("请输入公司名称");
                    return;
                }

                UpdateUserInfoRequest bean = new UpdateUserInfoRequest();
                bean.setPhone(SPUtils.getInstance().getString(Constants.SP_PHONE, ""));
                bean.setHeadUrl(SPUtils.getInstance().getString(Constants.SP_HEAD_URL, ""));
                bean.setUserName(SPUtils.getInstance().getString(Constants.SP_USER_NAME, ""));
                bean.setCompanyName(company);

                RequestBody body = getRequestBody(bean);
                api.doUpdateUserInfo(body).enqueue(new Callback<CommonResponse>() {
                    @Override
                    public void onResponse(Call<CommonResponse> call, Response<CommonResponse> response) {
                        if (response.body() != null) {
                            if (Constants.RESULT_OK.equals(response.body().getCode())) {
                                ToastUtils.showShort("公司名称修改成功");
                                SPUtils.getInstance().put(Constants.SP_COMPANY_NAME, company);
                                setResult(RESULT_OK);
                                mSwipeBackHelper.backward();
                            } else {
                                ToastUtils.showShort(response.body().getMsg());
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonResponse> call, Throwable t) {
                        ToastUtils.showShort(CompanyNameActivity.this.getResources().getString(R.string.call_api_error));
                    }
                });
            }
        });
    }


}
